/*The following program checks whether a number is armstrong number or not and also displays all armstrong numbers
from 1 to 1000. Armstrong numbers are such numbers whose sum of the cubes of digits is equal to the number itself.
For Example
371 is an Armstrong number
371=3*3*3+7*7*7+1*1*1
*/

/*
Reff Link : http://www.thelearningpoint.net/computer-science/c-program
*/

#include <stdio.h>

void check_armstrong (int num)
{
	int tmp, sum = 0, last;

	tmp = num;
	
	while (tmp > 0)
	{
		last = tmp % 10;
		sum += last * last * last;
		tmp = tmp / 10;
	}
	if (sum == num)
		printf("Given num is ArmStrong\n");
	else
		printf("NOT armstrong num\n");;
}

void list_armstrong (int num)
{
	int tmp, sum = 0, last, i;

	for (i=1; i <=num; i++)
	{
		tmp = i;
		sum = 0;
		
		while (tmp > 0)
		{
			last = tmp % 10;
			sum += last * last * last;
			tmp = tmp / 10;
		}
		if (sum == i)
			printf ("\nArmstrong num is %d\n", sum);
	}
}

main()
{
	int ch, num;

	printf("1-check armstrong nums\n2-list arstrong nums\t");
	scanf("%d", &ch);

	switch (ch)
	{
		case 1:
			printf("Enter num to check\t");
			scanf("%d", &num);
		
			check_armstrong(num);
			break;

		case 2:
			printf("Enter num to list armstrong nums\t");
			scanf("%d", &num);
			 
			list_armstrong (num);
			break;		
			
	}
}	
